package com.cbarmstrong.MarketWatcher;

import java.io.OutputStream;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Logger;

import com.cbarmstrong.Finance.Data;
import com.cbarmstrong.Finance.Indicator;
import com.cbarmstrong.Finance.Indicators;
import com.cbarmstrong.Finance.Ticker;
import com.cbarmstrong.Finance.TradeSet;

public class IndicatorProcessor implements Cloneable {
	
	private Ticker tickerObject;
	private Vector<Double> profit;
	private String name;
	private Vector<Indicator> inds=new Vector<Indicator>();
	static Logger logger=Data.getLogger();
	
	public TradeSet updateSet(){
		return execute();
	}
	
	public TradeSet execute() {
		//logger.fine("Starting tradeset");
		TradeSet ts=new TradeSet();
		for(int i=0;i<inds.lastElement().days();i++){
			//logger.fine("Deciding for "+tickerObject.getName()+" on "+tickerObject.getDates().get(i));
			int position=0;
			int compareSize=inds.size();
			for(int j=0;j<inds.size();j++){
				try{
					position+=inds.get(j).position(tickerObject.getDates().get(i));
				} catch (NullPointerException e){
					logger.finest("Null at "+i+" for "+inds.get(j).save());
					compareSize--;
				}
			}
			logger.finest("Position: "+position+", Indicators Compared: "+compareSize+" Indicator Count: "+inds.size()+" Current Position: "+ts.getPosition());
			if(ts.currentlyLong() && position!=compareSize){ 
				ts.closeTrade(tickerObject.getDates().get(i), tickerObject.getCloses().get(i));
			}
			if(ts.currentlyShort() && position!=(compareSize*-1)){ 
				ts.closeTrade(tickerObject.getDates().get(i), tickerObject.getCloses().get(i));
			}
			if(ts.currentlyOut()){
				if(position==compareSize){
					ts.openTrade(tickerObject.getDates().get(i), 1, tickerObject.getCloses().get(i), tickerObject.getSpread());
				}
				else if(position==(compareSize*-1)){
					ts.openTrade(tickerObject.getDates().get(i), -1, tickerObject.getCloses().get(i), tickerObject.getSpread());
				}
			}
		}
		profit=ts.getProfitVect();

		//logger.fine("Ending tradeset");
		return ts;
	}
	
	@SuppressWarnings("rawtypes")
	public String drawProfit(String uniqueID){
		Vector<Vector> sets=new Vector<Vector>();
		sets.add(profit);
		return tickerObject.makePicture(sets, 1024, 768, "Profit"+uniqueID);
	}
	
	@SuppressWarnings("rawtypes")
	public void writeProfit(OutputStream out){
		Vector<Vector> sets = new Vector<Vector>();
		sets.add(profit);
		tickerObject.writePicture(sets, 1024, 768, out);
	}
	
	public boolean addCrossIndicator(String s1, String s2){
		Indicator i=Indicators.getCrossOverIndicator(s1, s2, tickerObject);
		if(inds.contains(i)){ return false; }
		inds.add(i);
		return true;
	}
	
	public boolean addRangeIndicator(String s1, String series, String s2, int mode){
		Indicator i=Indicators.getRangeIndicator(s1, series, s2, mode, tickerObject);
		if(inds.contains(i)){ return false; }
		inds.add(i);
		return true;
	}
	
	public Ticker getTicker(){
		return tickerObject;
	}

	public void initiate(String ticker, int days) {
		try {
			logger.fine("Initiated with ticker for "+ticker);
			tickerObject=new Ticker(ticker, days);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void initiate(Ticker ticker) {
		tickerObject=ticker;
	}

	public void setName(String string) {
		this.name=string;
	}
	
	public String getName(){
		return this.name;
	}

	public void addIndicator(Indicator ind) {
		inds.add(ind);
	}

	public Vector<Indicator> getInds() {
		return inds;
	}
	
	public String save(){
		String ret="";
		for(Indicator ind : inds){
			ret+=ind.save()+":";
		}
		ret=ret.substring(0, ret.length()-1);
		return ret;
	}

}
